#AIQuestionAnswer_DEEPSEEK_aliyun.py

from aliyunsdkcore.client import AcsClient
from aliyunsdknlp_automl.request.v20191111 import RunPreTrainServiceRequest
import json
import requests

# 你的DeepSeek API密钥
DEEPSEEK_API_KEY = 'your_deepseek_api_key_here'
# DeepSeek API的基础URL
DEEPSEEK_BASE_URL = 'https://api.deepseek.com/v1/'

def call_deepseek_api(endpoint, data):
    """
    调用DeepSeek API的通用函数。
    
    :param endpoint: API端点（例如：'search', 'generate'等）
    :param data: 发送给API的数据
    :return: API返回的响应数据
    """
    url = f'{DEEPSEEK_BASE_URL}{endpoint}'
    headers = {
        'Authorization': f'Bearer {DEEPSEEK_API_KEY}',
        'Content-Type': 'application/json'
    }
    response = requests.post(url, json=data, headers=headers)
    
    if response.status_code == 200:
        return response.json()
    else:
        raise Exception(f"请求失败: {response.status_code}, {response.text}")

# 示例：调用搜索API
def deepseek_search_example():
    query = "人工智能"
    data = {
        'query': query,
        'num_results': 5
    }
    try:
        result = call_deepseek_api('search', data)
        print("DeepSeek搜索结果:", result)
    except Exception as e:
        print(f"错误: {e}")
        
        


# 阿里云Access Key ID和Access Key Secret
ALIYUN_ACCESS_KEY_ID = 'your_aliyun_access_key_id_here'
ALIYUN_ACCESS_KEY_SECRET = 'your_aliyun_access_key_secret_here'
# 创建AcsClient实例
client = AcsClient(ALIYUN_ACCESS_KEY_ID, ALIYUN_ACCESS_KEY_SECRET, 'cn-hangzhou')

def call_tongyi_api(prompt, model='Qwen-7B'):
    """
    调用阿里云通义大模型API。
    
    :param prompt: 输入文本
    :param model: 使用的大模型版本，默认为'Qwen-7B'
    :return: API返回的响应数据
    """
    request = RunPreTrainServiceRequest.RunPreTrainServiceRequest()
    request.set_ServiceCode('qwen')
    request.set_TaskType('text-generation')
    request.set_Text(prompt)
    request.set_Model(model)

    response = client.do_action_with_exception(request)
    return json.loads(response)

# 示例：调用通义大模型API
def tongyi_generate_example():
    prompt = "写一篇关于人工智能的文章。"
    try:
        result = call_tongyi_api(prompt)
        print("通义生成结果:", result)
    except Exception as e:
        print(f"错误: {e}")

if __name__ == "__main__":
    # 调用DeepSeek搜索API示例
    deepseek_search_example()

    # 调用通义大模型API示例
    tongyi_generate_example()     
        
        
